home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / ServiceController / Service.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  5.7 KB  |  187 lines

  1. unit Service;
  2. {*******************************************************************************
  3.   ServiceController Demo
  4.   Written by David Clegg, davidclegg@optusnet.com.au.
  5.  
  6.   Class to wrap and expose existing properties not exposed by the
  7.   ServiceController class.
  8. *******************************************************************************}
  9.  
  10. interface
  11.  
  12. uses
  13.   System.ServiceProcess, System.Management;
  14.  
  15. type
  16.   /// <summary>
  17.   /// The ServiceController class doesn't expose all the methods I would like,
  18.   /// so I am extending it to provide the missing information.
  19.   /// </summary>
  20.   TService = class(ServiceController)
  21.   private
  22.     FServiceObject: ManagementObject;
  23.     procedure InitServiceObject;
  24.     function GetDescription: string;
  25.     function GetExePath: string;
  26.     function GetStartMode: string;
  27.     procedure SetStartMode(const Value: string);
  28.     procedure ReallySetStartMode;
  29.   public
  30.     property Description: string read GetDescription;
  31.     property ExePath: string read GetExePath;
  32.     property StartMode: string read GetStartMode write SetStartMode;
  33.     procedure ShowProperties;
  34.     procedure Save;
  35.     constructor Create(const pServiceName: string); overload;
  36.     constructor Create(const pServiceName, pMachineName: string); overload;
  37.   end;
  38.  
  39. implementation
  40.  
  41. uses
  42.   SysUtils, System.Collections, System.Windows.Forms, System.Text;
  43.  
  44. /// <summary>
  45. /// Creates a new TService instance, quering WMI using the specified service
  46. /// name to retrieve additional properties not exposed by the
  47. /// ServiceController class.
  48. /// <param name='pServiceName'>The name of the Service.</param>
  49. /// </summary>
  50. constructor TService.Create(const pServiceName: string);
  51. begin
  52.   inherited Create(pServiceName);
  53.   InitServiceObject;
  54. end;
  55.  
  56. /// <summary>
  57. /// Creates a new TService instance, quering WMI using the specified service
  58. /// and machine names to retrieve additional properties not exposed by the
  59. /// ServiceController class.
  60. /// </summary>
  61. /// <param name='pServiceName'>The name of the Service.</param>
  62. /// <param name='pMachineName'>
  63. /// The name of the machine where the service resides.
  64. /// </param>
  65. constructor TService.Create(const pServiceName, pMachineName: string);
  66. begin
  67.   inherited Create(pServiceName, pMachineName);
  68.   InitServiceObject;
  69. end;
  70.  
  71. /// <summmary>
  72. /// Returns the detailed description for the service.
  73. /// </summary>
  74. function TService.GetDescription: string;
  75. begin
  76.   Result := '';
  77.   if Assigned(FServiceObject) then
  78.     if Assigned (FServiceObject['Description']) then
  79.       Result := FServiceObject['Description'].ToString;
  80. end;
  81.  
  82. /// <summary>
  83. /// Returns the path to the service's executable.
  84. /// </summary>
  85. function TService.GetExePath: string;
  86. begin
  87.   Result := '';
  88.   if Assigned(FServiceObject) then
  89.     if Assigned(FServiceObject['PathName']) then
  90.       Result := FServiceObject['PathName'].ToString;
  91. end;
  92.  
  93. /// <summary>
  94. /// Returns the start mode for the service.
  95. /// </summary>
  96. function TService.GetStartMode: string;
  97. begin
  98.   Result := '';
  99.   if Assigned(FServiceObject) then
  100.     if Assigned(FServiceObject['StartMode']) then
  101.       Result := FServiceObject['StartMode'].ToString;
  102. end;
  103.  
  104. /// <summmary>
  105. /// Create a ManagementObject to query WMI for additional information about
  106. /// the service.
  107. /// </summary>
  108. procedure TService.InitServiceObject;
  109. var
  110.   lPath: ManagementPath;
  111. begin
  112.   lPath := ManagementPath.Create(Format('Win32_Service.Name=''%s''', [ServiceName]));
  113.   lPath.Server := MachineName;
  114.   FServiceObject := ManagementObject.Create(lPath);
  115. end;
  116.  
  117. /// <summary>
  118. /// Save changes to the service. For now, the only property that can be changed
  119. /// is the start mode.
  120. /// </summary>
  121. procedure TService.Save;
  122. begin
  123.   if Assigned(FServiceObject) then
  124.     ReallySetStartMode;
  125. end;
  126.  
  127. /// <summary>
  128. /// Set the start mode for the service.
  129. /// </summary>
  130. /// <param name='Value'>The new start mode.</param>
  131. procedure TService.SetStartMode(const Value: string);
  132. begin
  133.   if Assigned(FServiceObject) then
  134.     FServiceObject['StartMode'] := Value;
  135. end;
  136.  
  137. /// <summary>
  138. /// Calls the ChangeStartMdoe method of the ManagementObject to change
  139. /// the service's start mode.
  140. /// </summary>
  141. procedure TService.ReallySetStartMode;
  142. var
  143.   lParam: string;
  144. begin
  145.   //Setting the 'StartMode' property of the ManagementObject instance doesn't
  146.   //actually update the property of the service application. The
  147.   //ChangeStartMode method must be invoked instead. I have seperated this out
  148.   //from the StartMode property setter, as that changes the 'StartMode'
  149.   //parameter so the GUI reflects the correctly chosen value, but the service
  150.   //application won't actually have the property changed until TService.Save
  151.   //is called.
  152.   if StartMode = 'Auto' then
  153.     //Even thought ManagementObject['StartMode'] returns 'Auto',
  154.     //ChangeStartMode expects 'Automatic'
  155.     lParam := 'Automatic'
  156.   else
  157.     lParam := StartMode;
  158.   FServiceObject.InvokeMethod('ChangeStartMode', [lParam]);
  159. end;
  160.  
  161. /// <summary>
  162. /// Shows all properties exposed by the ManagementObject class.
  163. /// </summary>
  164. procedure TService.ShowProperties;
  165. var
  166.   lProperty: PropertyData;
  167.   lProperties: PropertyDataCollection;
  168.   lEnumerator: IEnumerator;
  169.   lStringBuilder: StringBuilder;
  170. begin
  171.   if Assigned(FServiceObject) then
  172.   begin
  173.     lStringBuilder := StringBuilder.Create;
  174.     lProperties := FServiceObject.Properties;
  175.     lEnumerator := lProperties.GetEnumerator;
  176.     lEnumerator.Reset;
  177.     while lEnumerator.MoveNext do
  178.     begin
  179.       lProperty := lEnumerator.Current as PropertyData;
  180.       lStringBuilder.Append(Format('%s=%s', [lProperty.Name, lProperty.Value]) + #13#10);
  181.     end;
  182.     MessageBox.Show(lStringBuilder.ToString, Format('%s properties', [Self.DisplayName]));
  183.   end;
  184. end;
  185.  
  186. end.
  187.